package com.ueboot.weishop.service.customer.impl;

import com.ueboot.weishop.entity.customer.CustomerFavoritesEntity;
import com.ueboot.weishop.repository.customer.CustomerFavoritesRepository;
import com.ueboot.weishop.service.customer.CustomerFavoritesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * Created by Neel on 2017/9/24.
 */
@Slf4j
@Service
public class CustomerFavoritesServiceImpl implements CustomerFavoritesService {

    @Autowired
    private CustomerFavoritesRepository repository;


    @Override
    public Page<CustomerFavoritesEntity> findBy(Pageable pageable) {
        return repository.findAll(pageable);
    }

    /**
     * 根据关键字查询
     *
     * @param pageable
     * @param keyword
     * @return
     */
    @Override
    public Page<CustomerFavoritesEntity> findBy(Pageable pageable, String keyword) {
        CustomerFavoritesEntity entity = new CustomerFavoritesEntity();
        ExampleMatcher example = ExampleMatcher.matching()
                .withIgnoreNullValues()
                .withMatcher("name", matcher -> matcher.contains())
                .withMatcher("remark", matcher -> matcher.contains());
        return repository.findAll(Example.of(entity, example), pageable);
    }

    @Override
    public CustomerFavoritesEntity save(CustomerFavoritesEntity s) {
        return repository.save(s);
    }

    @Override
    public CustomerFavoritesEntity get(Long id) {
        return repository.findOne(id);
    }

    @Override
    public void delete(Long id) {
        repository.delete(id);
    }

    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            repository.delete(id);
        }
    }

}
